<?php
namespace Common\Model;

use Common\Model\BaseModel;

/**
 * 权限规则model
 */
class PageModel extends BaseModel
{

    /**
     * 初始化
     */
    public function __construct()
    {

    }

    /**
     * 首页
     */
    public function index($total_page)
    {

        //关键词搜索
        $keywords = I("get.keywords");
        $key_id = I("get.key_id", 0,'int');
        if (!empty($keywords)) {
            if ($key_id == 0) {
                $where['a.page_name'] = array("like", "%$keywords%");
            } else if ($key_id == 1) {
                $where['a.page_title'] = array("like", "%$keywords%");
            } else if ($key_id == 2) {
                $where['id'] = array("like", "%$keywords%");
            } else {
                $where['a.goods_id'] = array("like", "%$keywords%");
            }
        }

        //发布状态
        $page_state = I("get.page_state",3,'int');
        if ($page_state == 0) {
            $where['a.page_state'] = array("eq", 0);
        } else if ($page_state == 1) {
            $where['a.page_state'] = array("eq", 1);
        } else {
            $where['a.page_state'] = array("in", array(0, 1));
        }

        //页面状态
        $typeid = I("get.typeid", 0,'int');

        if ($typeid == 1) {
            $where['a.typeid'] = array("eq", 1);
        } else if ($typeid == 2) {
            $where['a.typeid'] = array("eq", 2);
        }

        //页码
        $start_page = I("get.p", 0, 'int');

        $where['a.user_id'] = array("eq", session('user.id'));

        $DB_PREFIX = strtolower(C('DB_PREFIX').'order');

        $info = M("addpage_info")->alias("a")->join("LEFT JOIN __GOODS__ b ON a.goods_id = b.id")->field("a.*,b.goods_name,(select sum(order_number) as cc from $DB_PREFIX b where a.id = b.addpage_info_id) as order_number")->where($where)->page($start_page, $total_page)->order("a.id desc")->select();

        $count = M("addpage_info")->alias("a")->where($where)->count();

        $pages = $this->pageClass($count, $total_page);

        $data = array(
            "list" => $info,
            "keywords" => $keywords,
            "page_state" => $page_state,
            "key_id" => $key_id,
            "typeid" => $typeid,
            "pages" => $pages
        );

        return $data;

    }

    /**
     * 移除页面
     */
    public function removePage()
    {

        $post = I("post.");

        $data = array(
            "page_state" => 2,
        );

        $where['id'] = array("eq", $post['id']);
        $where['user_id'] = array("eq", session('user.id'));


        $id = M("addpage_info")->where($where)->setField($data);

        if (empty($id)) {
            $rows = array(
                "code" => 400,
                "msg"=>"系统繁忙，请稍后重试!"
            );
        } else {
            $rows = array(
                "code" => 200,
                "msg"=>"OK"
            );
        }

        return $rows;
    }

    /**
     * 复制页面
     */
    public function copyPage()
    {

        $id = I("post.id",'','int');
        $page_name = I("post.page_name");

        //验证页面名称不能重复
        $map['user_id'] = array("eq",session('user.id'));
        $map['page_name']= array("eq",$page_name);

        $info = M("addpage_info")->field("id,typeid")->where($map)->find();

        if(empty($info)){

            //创建单品页
            $where["id"] = array("eq", $id);
            $where["user_id"] = array("eq", session('user.id'));
            $addpage_info = M("addpage_info")->where($where)->find();
            $addpage_info['page_name'] = $page_name;
            $addpage_info['page_state'] = 0;
            unset($addpage_info['id']);
            $addpage_info_id = M('addpage_info')->add($addpage_info);

            if($addpage_info['typeid'] == 1){

                try {

                    //商品套餐
                    $where = array();

                    $where['addpage_info_id'] = array("eq", $id);

                    $addpage_package = M("addpage_package")->where($where)->select();

                    foreach ($addpage_package as $package) {

                        $package_data = array(
                            "package_name" => $package['package_name'],
                            "package_price" => $package['package_price'],
                            "addpage_info_id" => $addpage_info_id
                        );

                        $addpage_package_id = M("addpage_package")->data($package_data)->add();

                        $package_product_list = M("addpage_package_product")->where("package_id=" . $package['id'])->select();

                        foreach ($package_product_list as $list) {

                            //添加 - 商品产品表
                            $product_data = array(
                                "product_name" => $list['product_name'],
                                "package_id" => $addpage_package_id
                            );

                            $product_id = M("addpage_package_product")->data($product_data)->add();

                            //添加 - 商品属性名称表
                            $attribute_name_list = M("addpage_package_attribute_name")->where("product_id=" . $list['id'])->select();

                            foreach ($attribute_name_list as $list) {

                                $attribute_name_data = array(
                                    "product_id" => $product_id,
                                    "attribute_name" => $list['attribute_name']
                                );

                                $attribute_name_id = M("addpage_package_attribute_name")->data($attribute_name_data)->add();

                                //属性值
                                $attribute_name_list = M("addpage_package_attribute_val")->where("attribute_name_id=" . $list['id'])->select();

                                foreach ($attribute_name_list as $list) {

                                    $attribute_val_data = array(
                                        "attribute_name_id" => $attribute_name_id,
                                        "attribute_val" => $list['attribute_val']
                                    );

                                    M("addpage_package_attribute_val")->data($attribute_val_data)->add();

                                }

                            }

                        }

                    }

                    //商品详情页
                    //获取上传首图
                    $where = array();
                    $where["addpage_info_id"] = array("eq", $id);
                    $where["user_id"] = array("eq", session('user.id'));

                    $details_images_serial_list = M("addpage_details_images_serial")->where($where)->select();

                    foreach ($details_images_serial_list as $list) {

                        $details_images_serial_data = array(
                            "addpage_info_id" => $addpage_info_id,
                            "typeid" => $list['typeid'],
                            "user_id" => session('user.id')
                        );

                        //获取图片设置类型
                        $images_serial_id = M("addpage_details_images_serial")->data($details_images_serial_data)->add();

                        //插入上传图片
                        $details_images_list = M("addpage_details_images")->where("images_serial_id=" . $list['id'])->select();

                        foreach ($details_images_list as $images) {

                            $details_images_data = array(
                                "image" => $images['image'],
                                "images_serial_id" => $images_serial_id
                            );

                            M("addpage_details_images")->data($details_images_data)->add();
                        }

                        //插入文字
                        $details_text_list = M("addpage_details_text")->where("images_serial_id=" . $list['id'])->select();

                        foreach ($details_text_list as $text) {
                            //插入图文设置表
                            $details_text_data = array(
                                "text" => $text['text'],
                                "images_serial_id" => $images_serial_id
                            );

                            M("addpage_details_text")->data($details_text_data)->add();

                        }

                    }


                    //插入首页设置表
                    $details_index = M("addpage_details_index")->where($where)->find();

                    $details_index_data = array(
                        "index_buy_name" => $details_index['index_buy_name'],
                        "index_price" => $details_index['index_price'],
                        "index_buy_bg_color" => $details_index['index_buy_bg_color'],
                        "index_buy_text_color" => $details_index['index_buy_text_color'],
                        "index_buy_height" => $details_index['index_buy_height'],
                        "index_buy_hide" => $details_index['index_buy_hide'],
                        "index_price_text_color" => $details_index['index_price_text_color'],
                        "index_price_hide" => $details_index['index_price_hide'],
                        "index_spee_number" => $details_index['index_spee_number'],
                        "addpage_info_id" => $addpage_info_id,
                        "user_id" => session('user.id')
                    );

                    M("addpage_details_index")->data($details_index_data)->add();

                    unset($where["user_id"]);

                    $details_button = M("addpage_details_button")->where($where)->find();

                    //插入按钮设置
                    $details_button_data = array(
                        "button_form_name" => $details_button['button_form_name'],
                        "button_form_height" => $details_button['button_form_height'],
                        "button_phone_name" => $details_button['button_phone_name'],
                        "button_phone_number" => $details_button['button_phone_number'],
                        "button_phone_hide" => $details_button['button_phone_hide'],
                        "button_sms_name" => $details_button['button_sms_name'],
                        "button_sms_number" => $details_button['button_sms_number'],
                        "button_sms_hide" => $details_button['button_sms_hide'],
                        "button_buy_name" => $details_button['button_buy_name'],
                        "button_buy_hide" => $details_button['button_buy_hide'],
                        "button_bg_color" => $details_button['button_bg_color'],
                        "button_text_color" => $details_button['button_text_color'],
                        "button_qq_hide" => $details_button['button_qq_hide'],
                        "addpage_info_id" => $addpage_info_id,
                    );


                    M("addpage_details_button")->data($details_button_data)->add();

                    $data = array(
                        "code"=>200,
                        "msg"=>"复制成功",
                    );

                } catch (Exception $e) {
                    $data = array(
                        "code"=>400,
                        "msg"=>"系统繁忙，请稍后重试!",
                    );
                }

            }else{

                try {

                    //商品详情页
                    $where = array();
                    $where["addpage_info_id"] = array("eq", $id);
                    $where["user_id"] = array("eq", session('user.id'));

                    $details_images_serial_list = M("addpage_details_images_serial")->where($where)->select();

                    foreach ($details_images_serial_list as $list) {

                        $details_images_serial_data = array(
                            "addpage_info_id" => $addpage_info_id,
                            "typeid" => $list['typeid'],
                            "user_id" => session('user.id'),
                            "class_id" => $list['class_id'],
                        );

                        //获取图片设置类型
                        $images_serial_id = M("addpage_details_images_serial")->data($details_images_serial_data)->add();

                        //插入上传图片
                        $details_images_list = M("addpage_details_images")->where("images_serial_id=" . $list['id'])->select();

                        foreach ($details_images_list as $images) {

                            $details_images_data = array(
                                "image" => $images['image'],
                                "images_serial_id" => $images_serial_id
                            );

                            M("addpage_details_images")->data($details_images_data)->add();
                        }

                        //插入文字
                        $details_text_list = M("addpage_details_text")->where("images_serial_id=" . $list['id'])->select();

                        foreach ($details_text_list as $text) {
                            //插入图文设置表
                            $details_text_data = array(
                                "text" => $text['text'],
                                "images_serial_id" => $images_serial_id
                            );

                            M("addpage_details_text")->data($details_text_data)->add();

                        }

                        //插入聚合页
                        $addpage_group_list = M("addpage_group")->where("images_serial_id=" . $list['id'])->select();

                        foreach ($addpage_group_list as $list) {
                            //插入图文设置表
                            $addpage_group_data = array(
                                "addpage_info_id" => $list['addpage_info_id'],
                                "images_serial_id" => $images_serial_id
                            );

                            M("addpage_group")->data($addpage_group_data)->add();

                        }

                    }

                    $data = array(
                        "code"=>200,
                        "msg"=>"复制成功",
                    );

                } catch (Exception $e) {
                    $data = array(
                        "code"=>400,
                        "msg"=>"系统繁忙，请稍后重试!",
                    );
                }



            }

        }else{

            $data = array(
                "code"=>401,
                "msg"=>"页面名称不能重复",
            );

        }

        return $data;

    }


}
